using Assets.IOLibrary;
using Assets.IOLibrary.Model;
using Assets.Scripts;
using DataSourceLibray;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Networking.Types;
using UnityEngine.UI;

public class PreAlarm : BaseScripts
{
    public GameObject PreGameObject10x;
    public GameObject PreGameObject20x;
    public Transform parentTransform;

    private List<string> preAlarmList = new List<string>();

    public void ShowAlarm(string msg, string typeName, object sender)
    {
        var type = sender.GetType();
        if (type.Name == nameof(PLC10x))
        {
            ShowAlarm10X(msg, typeName, 0, sender as PLC10x);
        }
        else
        {
            ShowAlarm20X(msg, typeName, 0, sender as PLC20x);
        }
    }

    public void ShowAlarm20X(string msg, string typeName, int index, PLC20x plc20x)
    {
        GameObject preAlarm = null;
        Transform transformPanel = null;
        if (!preAlarmList.Contains(typeName))
        {
            preAlarmList.Add(typeName);

            preAlarm = Instantiate(PreGameObject20x, parentTransform);
            preAlarm.transform.localPosition = new Vector3(200, 0, 0);
            preAlarm.name = typeName;
            transformPanel = preAlarm.transform.Find("Panel20x");
            var btnclose = transformPanel.Find("BtnClose").GetComponent<Button>();
            btnclose.onClick.AddListener(() =>
            {
                preAlarm.SetActive(false);
                Destroy(preAlarm);
                preAlarmList.Remove(typeName);
            });
        }
        else
        {
            var obj = Resources.FindObjectsOfTypeAll<GameObject>().Where(m => m.name == typeName);
            if (obj.Count() == 0)
            {
                return;
            }
            preAlarm = obj.First();
            transformPanel = preAlarm.transform.Find("Panel20x");
        }

        transformPanel.Find("Text").GetComponent<Text>().text = typeName;
        transformPanel.Find("Img_DI1").GetComponent<Image>().color = plc20x.DI1 ? Color.green : Color.red;
        transformPanel.Find("Img_DI2").GetComponent<Image>().color = plc20x.DI2 ? Color.green : Color.red;
        transformPanel.Find("Img_DI3").GetComponent<Image>().color = plc20x.DI3 ? Color.green : Color.red;
        transformPanel.Find("Img_DO1").GetComponent<Image>().color = plc20x.DO1 ? Color.green : Color.red;
        transformPanel.Find("Img_DO2").GetComponent<Image>().color = plc20x.DO2 ? Color.green : Color.red;
        transformPanel.Find("Img_Alarm").GetComponent<Image>().color = Color.red;
        //preAlarm.transform.SetSiblingIndex(index);
        if (index == 0)
        {
            preAlarm.transform.SetAsLastSibling();
        }



    }
    public void ShowAlarm10X(string msg, string typeName, int index, PLC10x plc10x)
    {
        GameObject preAlarm = null;
        Transform transformPanel = null;
        if (!preAlarmList.Contains(typeName))
        {
            preAlarmList.Add(typeName);
            preAlarm = Instantiate(PreGameObject10x, parentTransform);
            preAlarm.name = typeName;
            preAlarm.transform.localPosition = new Vector3(-400, 0, 0);
            transformPanel = preAlarm.transform.Find("Panel10x");
            var btnclose = transformPanel.Find("BtnClose").GetComponent<Button>();
            btnclose.onClick.AddListener(() =>
            {
                preAlarm.SetActive(false);
                Destroy(preAlarm);
                preAlarmList.Remove(typeName);
            });
        }
        else
        {
            var obj = Resources.FindObjectsOfTypeAll<GameObject>().Where(m => m.name == typeName);
            if (obj.Count() == 0)
            {
                return;
            }
            preAlarm = obj.First();
            transformPanel = preAlarm.transform.Find("Panel10x");
        }

        transformPanel.Find("Text").GetComponent<Text>().text = typeName;
        transformPanel.Find("Img_DI1").GetComponent<Image>().color = plc10x.DI1 ? Color.green : Color.red;
        transformPanel.Find("Img_DI2").GetComponent<Image>().color = plc10x.DI2 ? Color.green : Color.red;
        transformPanel.Find("Img_DO1").GetComponent<Image>().color = plc10x.DO1 ? Color.green : Color.red;
        transformPanel.Find("Img_Alarm").GetComponent<Image>().color = Color.red;
        if (index == 0)
        {
            preAlarm.transform.SetAsLastSibling();
        }
    }


    void Start()
    {
        //StartCoroutine(nameof(UpdateUI));
        InvokeRepeating(nameof(UpdateUI), 0, 1);
    }

    void UpdateUI()
    {
        //yield return new WaitForSeconds(1f);

        foreach (var name in preAlarmList)
        {
            switch (name)
            {
                case nameof(PLC.PLC101):
                    ShowAlarm10X("", nameof(PLC.PLC101), -1, AppSettings.Instance.Io.PLC.PLC101);
                    break;
                case nameof(PLC.PLC102):
                    ShowAlarm10X("", nameof(PLC.PLC102), -1, AppSettings.Instance.Io.PLC.PLC102);
                    break;
                case nameof(PLC.PLC103):
                    ShowAlarm10X("", nameof(PLC.PLC103), -1, AppSettings.Instance.Io.PLC.PLC103);
                    break;
                case nameof(PLC.PLC201):
                    ShowAlarm20X("", nameof(PLC.PLC201), -1, AppSettings.Instance.Io.PLC.PLC201);
                    break;
                case nameof(PLC.PLC202):
                    ShowAlarm20X("", nameof(PLC.PLC202), -1, AppSettings.Instance.Io.PLC.PLC202);
                    break;
            }
        }
    }


}
